import axios from 'axios'
import { ref, computed } from 'vue'

//  1.获取数据
const list = ref([])

const getList = async () => {
    const res = await axios({ url: 'https://applet-base-api-t.itheima.net/bill?creator=ivan520' })
    console.log('66', res.data.data);
    list.value = res.data.data
}

// 计算总价
const totalPrice = computed(() => {
    let total = 0
    list.value.forEach(item => {
        total += item.price
    })
    return total
})



// 删除数据
const delList = async (id) => {
    await axios({
        url: `https://applet-base-api-t.itheima.net/bill/${id}`,
        method: 'DELETE'
    })
    getList()
}

const formData = ref({
    creator: 'ivan520',
    name: '',
    price: 0
})

// 添加数据
const addList = async () => {
    await axios({
        url: 'https://applet-base-api-t.itheima.net/bill',
        method: 'POST',
        data: formData.value
    })
    getList()
}



// 使用具名导出
export { list, getList, totalPrice, delList, addList,formData }